package com.zunipe.chat.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.zunipe.chat.R;
import com.zunipe.chat.bean.Message;
import com.zunipe.chat.util.TimeUtils;

import java.util.ArrayList;
import java.util.List;

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ChatViewHolder> {
    private final List<Message> messagesList;
    private Context context;

    public ChatAdapter(List<Message> messagesList) {
        this.messagesList = messagesList;
    }

    @NonNull
    @Override
    public ChatViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_layout, parent, false);
        context = parent.getContext();
        return new ChatViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ChatViewHolder holder, int position) {
        Message message = messagesList.get(position);

        holder.textView.setText(context.getString(R.string.chat, TimeUtils.getTime(message.getTime()), message.getSender(), message.getMsg()));
    }

    @Override
    public int getItemCount() {
        return messagesList.size();
    }

    public void addMessage(Message message) {
        //messagesList.add(message);
        notifyItemInserted(messagesList.size() - 1);
    }

    public static class ChatViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public ChatViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.textView);
        }
    }
}
